<template>
  <div class="g-input">
    <textarea :style="style" :placeholder="placeholder" :value="value" @input="input" @change="change" @blur="blur" @focus="focus" v-if="textarea"></textarea>
    <input :type="inputType" :style="style" :placeholder="placeholder" :value="value" @input="input" @change="change" @blur="blur" @focus="focus" v-else>
    <i class="iconfont icon" :class="icon" v-if="icon"></i>
    <i class="iconfont icon-guanbi clearable" v-if="clearable && value" @click="clearValue"></i>
    <i class="iconfont change-type" :class="{'icon-chakan': inputType === 'password','icon-chakan1': inputType === 'text'}" v-if="seepassword" @click="changeInputType"></i>
  </div>
</template>

<script>
export default {
  props: {
    type: {
      type: String
    },
    width: {
      type: Number,
      default: 300
    },
    height: {
      type: Number,
      default: 50
    },
    icon: {
      type: String
    },
    placeholder: {
      type: String
    },
    clearable: {
      type: Boolean
    },
    value: String,
    seepassword: {
      type: Boolean,
      defalut: false
    },
    textarea: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      inputType: this.type
    }
  },
  computed: {
    style() {
      return {
        width: this.width + "px",
        textIndent: this.icon ? "35px" : "10px",
        height: this.height + "px"
      }
    },
    form() {
      return this.$parent.$options.name === "gFormItem"
    }
  },
  methods: {
    clearValue() {
      this.$emit("input", "")
      if (this.form) this.$parent.$emit("g.form.input", event.target.value)
      if (this.form) this.$parent.$emit("g.form.change", event.target.value)
      if (this.form) this.$parent.$emit("g.form.blur", event.target.value)
      if (this.form) this.$parent.$emit("g.form.focus", event.target.value)
    },
    changeInputType() {
      if (!this.value) return
      if (this.inputType === "password") {
        this.inputType = "text"
      } else if (this.inputType === "text") {
        this.inputType = "password"
      }
    },
    input(event) {
      this.$emit("input", event.target.value)
      if (this.form) this.$parent.$emit("g.form.input", event.target.value)
    },
    change(event) {
      this.$emit("change", event.target.value)
      if (this.form) this.$parent.$emit("g.form.change", event.target.value)
    },
    blur(event) {
      this.$emit("blur", event.target.value)
      if (this.form) this.$parent.$emit("g.form.blur", event.target.value)
    },
    focus(event) {
      this.$emit("focus", event.target.value)
      if (this.form) this.$parent.$emit("g.form.focus", event.target.value)
    }
  }
}
</script>

<style lang="less">
.g-input {
  display: inline-block;
  margin-bottom: 20px;
  position: relative;
  textarea {
    color: #808080;
    border: 1px solid #e6e6e6;
    box-sizing: border-box;
    border-radius: 3px;
    padding: 10px;
  }
  input {
    line-height: 40px;
    height: 40px;
    border: 1px solid #e6e6e6;
    border-radius: 3px;
    box-sizing: border-box;
    color: #808080;
  }
  .icon {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: #808080;
  }
  .clearable {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    color: #808080;
    cursor: pointer;
    font-size: 10px;
  }
  .change-type {
    position: absolute;
    right: 30px;
    top: 50%;
    transform: translateY(-50%);
    color: #808080;
    cursor: pointer;
    font-size: 12px;
  }
}

input {
  outline: none;
}
</style>
